package com.fxd.rocketmq.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author xudongfu
 * @date 2023/02/27
 */
@Configuration
public class ApplicationEventMulticasterConfig {

    @Bean
    public ApplicationEventMulticaster applicationEventMulticaster() {
        // 创建一个事件广播器
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();

        // 给广播器提供一个线程池，通过这个线程池来调用事件监听器
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(8,
                100, 1, TimeUnit.MINUTES,
                new LinkedBlockingQueue<Runnable>());

        // 设置异步执行器
        simpleApplicationEventMulticaster.setTaskExecutor(threadPoolExecutor);//@1
        return simpleApplicationEventMulticaster;
    }
}
